from time import sleep


class Character:
    def __init__(self, name, sex, blood=100):
        self.name = name
        self.sex = sex
        self.blood = blood
        self.room = None
        self.equip = None

    def attack(self, enemy):
        if enemy.equip == None:
            self.damage = 10
        else:
            self.damage = 5
        print(f"{self.name}攻击了{enemy.name}{self.damage}点血量")
        enemy.blood -= self.damage


class Room:
    def __init__(self, name):
        self.name = name
        self.member = []

    def joinRoom(self, character):
        if character.room == None:
            if len(self.member) < 2:
                self.member.append(character)
                character.room = self.name
                print(f"{character.name}已成功加入{self.name}房间")
            else:
                print(f"{self.name}房间已满")
        else:
            print(f"{character.name}已在{character.room}房间！")


characters = {}
rooms = {}
while 1:
    print("*" * 20)
    print("请输入你的选择：")
    option = input("1、创建角色\n2、创建房间\n3、加入房间\n4、开始战斗\n5、退出游戏\n")
    if option == "1":
        while 1:
            name = input("请输入角色名字：")
            if name in characters:
                print(f"{name}已存在，请重新输入")
            else:
                sex = input("请输入角色性别：")
                characters[name] = Character(name, sex)
                print(f"角色{name}创建成功")
                break
    elif option == "2":
        while 1:
            room_name = input("请输入房间名：")
            if room_name in rooms:
                print(f"{room_name}已存在，请重新输入")
            else:
                rooms[room_name] = Room(room_name)
                print(f"房间{room_name}创建成功")
                break
    elif option == "3":
        if rooms == {}:
            print("目前没有房间，快去创建一个房间吧！")
        else:
            print(f"已有角色：{characters.keys()}")
            print(f"已有房间：{rooms.keys()}")
            while 1:
                character = input("请输入你的角色：")
                room = input("请输入你要加入的房间：")
                if character in characters and room in rooms:
                    rooms[room].joinRoom(characters[character])
                    break
                else:
                    print("输入有误，请重新输入")
    elif option == "4":
        while 1:
            print(f"已有房间：{rooms.keys()}")
            attckroom = input("请输入房间号(按q退出):")
            if attckroom != 'q':
                if attckroom in rooms:
                    if len(rooms[attckroom].member) == 2:
                        while 1:
                            if rooms[attckroom].member[0].blood <= 0:
                                print(f"{rooms[attckroom].member[0].name}被击败，游戏结束")
                                break
                            elif rooms[attckroom].member[1].blood <= 0:
                                print(f"{rooms[attckroom].member[1].name}被击败，游戏结束")
                                break
                            else:
                                rooms[attckroom].member[0].attack(rooms[attckroom].member[1])
                                sleep(1)
                                rooms[attckroom].member[1].attack(rooms[attckroom].member[0])
                    else:
                        print(f"{attckroom}房间人数不足，无法开始战斗，请重新选择")
                else:
                    print(f"{attckroom}房间不存在，请重新选择：")
            else:
                break
    elif option == "5":
        print("已退出")
        break
    else:
        print("输入有误，请重新输入")
